package com.quectel.business.contentaudit.baidu;

import com.quectel.business.common.thirdconfig.ThirdPlatformConfigBusiness;
import com.quectel.business.contentaudit.Validator;
import com.quectel.business.contentaudit.ValidatorContext;
import com.quectel.core.module.thirdconfig.dto.item.BaiduBCE;
import com.quectel.sdk.baidu.BaiduBCEClientBuilder;
import com.quectel.sdk.baidu.BaiduBCERequester;
import com.quectel.sdk.baidu.model.text.BaiduBCETextResp;
import com.quectel.sdk.baidu.text.BaiduBCETextClient;
import com.quectel.util.common.JacksonUtils;
import com.quectel.util.common.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/5/15 14:39
 * @description new java file header...
 */
@Component
@Slf4j
public class BaiduBCETextValidatorImpl implements Validator<ValidatorContext> {
    public static final int BAIDU_BCE_TEXT= 1;

    @Autowired
    private ThirdPlatformConfigBusiness thirdPlatformConfigBusiness;

    @Override
    public Response validate(ValidatorContext validatorContext) {

        log.debug("BaiduBCEValidatorImpl validate begin");

        BaiduBCE baiduBCEConfig = thirdPlatformConfigBusiness.getBaiduBCEConfig(validatorContext.getTenantId());
        if (baiduBCEConfig == null || !baiduBCEConfig.getTextEnable()) {
            return Response.ok();
        }
        BaiduBCETextClient bceTextClient = BaiduBCEClientBuilder.builder().host(BaiduBCERequester.HOST).apikey(baiduBCEConfig.getApiKey()).secretKey(baiduBCEConfig.getSecretKey()).build(BaiduBCETextClient.class);

        Response textResponse = checkText(bceTextClient, validatorContext.getText());
        if (!textResponse.isSuccess()) {
            return textResponse;
        }

        log.debug("BaiduBCEValidatorImpl validate end");

        return Response.ok();
    }

    @Override
    public int getPriority() {
        return BAIDU_BCE_TEXT;
    }

    @Override
    public Response invalidCache(ValidatorContext validatorContext) {
        return Response.ok();
    }

    private Response checkText(BaiduBCETextClient client, String text) {
        Response response = client.requestPreCheck(text);
        if (!response.isSuccess()) {
            return response;
        }
        BaiduBCETextResp check = client.check(text);
        if (check == null) {
            return Response.error("请求第三方接口异常");
        }
        if (StringUtils.isNotBlank(check.getErrorCode()) || StringUtils.isNotBlank(check.getErrorMsg())) {
            return Response.error("请求第三方接口异常，errorCode：{" + check.getErrorCode() + "}，errorMsg：{" + check.getErrorMsg() + "}");
        }
        log.debug("checkText response : {}", JacksonUtils.toJsonString(check));
        if (check.getConclusionType() != BaiduBCETextClient.PASS_CHECK) {
            return Response.error(check.getConclusion());
        }

        return Response.ok();
    }
}
